@extends('shop::layouts.master')
@section('title')
    {{$title}}
@endsection
@css(css/goods.css)
{{--<link rel="stylesheet" href="/css/formSelects-v4.css" />--}}
@section('tab')
    <form class="layui-form">
        <div class="goods_fax">
            <div class="layui-row">
                <div class="layui-tab layui-tab-brief" lay-filter="filter">
                    <ul class="layui-tab-title">
                        <li class="layui-this">商品信息</li>
                        <li id="specsShow">规格属性</li>
                        <li id="img_info">商品相册</li>
                    </ul>
                    <div class="layui-tab-content">
                        {{--商品信息开始--}}
                        <div class="layui-tab-item layui-show">
                            <input type="hidden" name="goods[id]" value="{{$arr_data['id']}}">
                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>商品名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="goods[name]" value="{{$arr_data['name']}}" placeholder="商品名称" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>副标题</label>
                                <div class="layui-input-block">
                                    <input type="text" name="goods[title]" value="{{$arr_data['title']}}" placeholder="副标题" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>是否上架</label>
                                <div class="layui-input-block">
                                    <select name="goods[status]" class="goods_cate" lay-verify="required">
                                        <option value="">请选择</option>
                                        <option value="1" @if($arr_data['status']==1) selected @endif>上架</option>
                                        <option value="2" @if($arr_data['status']==2) selected @endif>下架</option>
                                    </select>
                                </div>
                            </div>

                            {{--<div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">保养分类</label>
                                <div class="layui-inline" style="width: 29%">
                                    <select name="goods[maintain_pid]" lay-filter="maintain_father">
                                        <option value="">请选择一级分类</option>
                                        @foreach($maintain as $key=>$value)
                                            <option value="{{$value['id']}}" @if($arr_data['maintain_pid']==$value['id']) selected @endif>{{$value['name']}}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="layui-inline" style="width: 30%">
                                    <select name="goods[maintain_id]" id="maintain_son">
                                        <option value="">请选择二级分类</option>
                                        @if($son)
                                        @foreach($son as $y=>$e)
                                            <option value="{{$e['id']}}" @if($arr_data['maintain_id']==$e['id']) selected @endif>{{$e['name']}}</option>
                                        @endforeach
                                        @endif
                                    </select>
                                </div>
                            </div>--}}

                            <div class="layui-form-item">
                                <label class="layui-form-label"><span class="x-red">*</span>服务标签</label>
                                <div class="layui-input-block" id="tags" data-tags="{{$sale_tag}}" data-old="{{$arr_data['sale_tag_relation']}}"></div>
                            </div>

                            {{--<div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>服务标签</label>
                                <div class="layui-input-block">
                                    <select name="sale_tag" lay-verify="required" multiple="multiple" xm-select="sale_tag" xm-select-skin="default" xm-select-show-count="6">
                                        @foreach($sale_tag as $tag)
                                            <option value="{{$tag['id']}}">{{$tag['name']}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>--}}

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">是否包邮</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="goods[is_free]" value="1" title="不包邮费" @if($arr_data['is_free']==1) checked @endif>
                                    <input type="radio" name="goods[is_free]" value="2" title="包邮费" @if($arr_data['is_free']==2) checked @endif>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">是否推荐</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="goods[is_recommend]" value="0" title="不推荐" @if($arr_data['is_recommend']==0) checked @endif>
                                    <input type="radio" name="goods[is_recommend]" value="1" title="推荐" @if($arr_data['is_recommend']==1) checked @endif>
                                </div>
                            </div>

                           {{-- <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>商品类型</label>
                                <div class="layui-input-block">
                                    <input type="hidden" value="{{$arr_data['type']}}" id="goods_type">
                                    <input type="radio" name="goods[type]" lay-filter="goodsCate" value="1" disabled title="普通商品" @if($arr_data['type']==1) checked @endif>
                                    <input type="radio" name="goods[type]" lay-filter="goodsCate" value="2" disabled title="积分商品" @if($arr_data['type']==2) checked @endif>
                                    <input type="radio" name="goods[type]" value="3" disabled title="活动商品" @if($arr_data['type']==3) checked @endif>
                                </div>
                            </div>--}}

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>商城分类</label>
                                <div class="layui-input-block">
                                    <select name="goods[cate_id]" class="goods_cate" lay-verify="required">
                                        <option value="">请选择</option>
                                        @if($cate)
                                            @foreach($cate as $ca => $te)
                                                <option value="{{$te['id']}}" @if($arr_data['cate_id']==$te['id']) selected @endif>{{$te['name']}}</option>
                                            @endforeach
                                        @endif
                                    </select>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>商品图片</label>
                                <div class="layui-input-block">
                                <span class="pic_left">
                                    <input type="text" name="goods[pic_url]" value="{{$arr_data['pic_url']['src']}}" lay-verify="required" class="layui-input" readonly="readonly" id="img_url" placeholder="请长传图片大小为800x800，格式为jpg或者png">
                                </span>
                                    <span class="pic_right">
                                    <button type="button" class="layui-btn layui-btn-xs layui-btn-normal upimg">上传</button>
                                    <img class="show_img" style="" src="admin/images/upload.png">
                                    <img class="layui-upload-img is_show" src="{{$arr_data['pic_url']['full_src']}}">
                                </span>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>展示价格</label>
                                <div class="layui-input-block">
                                    <input type="text" name="goods[price]" value="{{$arr_data['price']}}" placeholder="展示价格" lay-verify="required|number" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>价格说明</label>
                                <div class="layui-input-block">
                                    <textarea name="goods[price_indication]" placeholder="请输入内容" lay-verify="required" class="layui-textarea">{{$arr_data['price_indication']}}</textarea>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>售后服务</label>
                                <div class="layui-input-block">
                                    <textarea name="goods[sale]" placeholder="请输入内容" lay-verify="required" class="layui-textarea">{{$arr_data['sale']}}</textarea>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>图文详情</label>
                                <div class="layui-input-block">
                                    {!! baidu_ueditor($arr_data['detail'], 'goods[detail]') !!}
                                </div>
                            </div>

                        </div>
                        {{--商品信息结束--}}

                        {{--规格属性开始--}}
                        <div class="layui-tab-item">

                            <div class="layui-form-item">
                                <label for="L_alias" class="layui-form-label">
                                    <span class="x-red">*</span>规格分类</label>
                                <div class="layui-input-block"style="width:200px">
                                    <select name="specs_cate_id" class="goods_cate" lay-verify="required">
                                        <option value="">选择规格</option>
                                        @foreach($specs as $ky => $vl)
                                            <option value="{{$vl['id']}}" @if($arr_data['specs_cate_id']==$vl['id']) selected @endif>{{$vl['name']}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <span>
                                @if($arr_data['product'])
                                    @foreach($arr_data['product'] as $pr => $pro)
                                    <div class="layui-form-item">
                                        <div class="layui-inline" style="width: 100%">
                                            <input type="hidden" name="product[{{$pr}}][id]" value="{{$pro['id']}}">
                                            <label for="L_alias" class="layui-form-label">
                                                <span class="x-red">*</span>规格属性</label>
                                            <div class="layui-input-inline" style="width:45%">
                                                <input type="hidden" name="product[{{$pr}}][name_id]" value="{{$pro['specs']['id']}}">
                                                <input type="text" name="product[{{$pr}}][name]" value="{{$pro['specs']['name']}}" placeholder="规格属性" lay-verify="required" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline" style="width:8%">
                                                <input type="text" name="product[{{$pr}}][price]" value="{{$pro['price']}}" placeholder="价格" lay-verify="required" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline integral" style="width:8%;@if($arr_data['type']!=2) display: none; @endif">
                                                <input type="text" name="product[{{$pr}}][integral]" value="{{$pro['integral']}}" placeholder="积分" lay-verify="@if($arr_data['type'] == 2 ) required @endif" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline" style="width:8%">
                                                <input type="text" name="product[{{$pr}}][stock]" value="{{$pro['stock']}}" placeholder="库存" lay-verify="required" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline specs_fex{{$pr}} product" style="width:50px">
                                                <input type="hidden" name="product[{{$pr}}][pic]" value="{{$pro['pic']['src']}}" class="specs_url">
                                                <img class="specs_pic" src="{{$pro['pic']['full_src']}}">
                                            </div>
                                            @if($pr == 0)
                                                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm add_specs" da-in="{{$pr}}">
                                                    <i class="layui-icon"></i>
                                                </button>
                                            @else
                                                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm delelt" del-id="{{$pro['id']}}"><i class="layui-icon"></i></button>
                                            @endif
                                        </div>
                                    </div>
                                    @endforeach
                                @else
                                    <div class="layui-form-item">
                                        <div class="layui-inline" style="width: 100%">
                                            <label for="L_alias" class="layui-form-label">
                                                <span class="x-red">*</span>规格属性</label>
                                            <div class="layui-input-inline" style="width:45%">
                                                <input type="text" name="product[0][name]" value="" placeholder="规格属性" lay-verify="required" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline" style="width:8%">
                                                <input type="text" name="product[0][price]" value="" placeholder="价格" lay-verify="required" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline" style="width:8%">
                                                <input type="text" name="product[0][integral]" value="" placeholder="积分" lay-verify="@if($arr_data['type'] == 2 ) required @endif" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline" style="width:8%">
                                                <input type="text" name="product[0][stock]" value="" placeholder="库存" lay-verify="required" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline specs_fex product" style="width:50px">
                                                <input type="hidden" name="product[0][pic]" value="" class="specs_url">
                                                <img class="specs_pic" src="admin/images/upload.png">
                                            </div>
                                            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm add_specs" da-in="0">
                                                <i class="layui-icon"></i>
                                            </button>
                                        </div>
                                    </div>
                                @endif

                            </span>
                        </div>
                        {{--规格属性结束--}}

                        {{--商品相册开始--}}
                        <div class="layui-tab-item">
                            <div class="layui-upload">
                                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;border-width: 1px">
                                    <div class="layui-upload" lay-verify="img_num" id="picNum">
                                        @if($arr_data['img'])
                                        @foreach($arr_data['img'] as $i => $item)
                                        <span class="img-list">
                                            <input type="hidden" name="img[{{$i}}][id]" value="{{$item['id']}}">
                                            <input type="hidden" name="img[{{$i}}][url]" value="{{$item['picurl']['full_src']}}">
                                            <img src="{{$item['picurl']['full_src']}}" class="layui-upload-img imgNum">
                                            <i class="layui-icon close del-img" del-id="{{$item['id']}}">ဆ</i>
                                        </span>
                                        @endforeach
                                        @endif
                                        <span id="imgLst"></span>
                                        {{--<img src="admin/images/upimg.png" id="edit_more_img" class="layui-upload-img" style="cursor:pointer;">--}}
                                    </div>
                                </blockquote>
                                <button type="button" class="layui-btn" id="edit_more_img">选择图片</button>
                            </div>
                        </div>
                        {{--商品相册结束--}}
                        <div class="layui-form-item">
                            <label for="L_repass" class="layui-form-label"></label>
                        </div>

                    </div>
                </div>
            </div>
        </div>
        <div class="layui-footer">
            <div class="layui-main">
                <button class="layui-btn" lay-filter="add" lay-submit="">编辑</button>
            </div>
        </div>
    </form>
@endsection

@section('tab_js')
    {{--<script src="/js/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>--}}
    <script src="/js/selectM.js" type="text/javascript" charset="utf-8"></script>
    @include('shop::goods.this_js')
    <script>
        layui.use(['form', 'layer', 'jquery'], function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
            var selectM = layui.selectM;
            var tagData = $('#tags').data('tags');
            var selected = $('#tags').data('old');
            selectM({
                elem: '#tags'//元素容器【必填】
                , data: tagData//候选数据【必填】
                , selected: selected
                , name: 'sale_tag'
                , verify: 'required'//添加验证
            });

            $('dd').css('padding', '3px 10px');
            form.render('select');

            form.verify({
                goods_pic : function(value) {
                    if (value.length == '') {
                        $("#specsShow").trigger("click");
                        return '规格属性图片不能为空';
                    }
                },
                img_num : function(value) {
                    var num = $("#picNum").children("span").length;
                    if(num <= 1){
                        $("#img_info").trigger("click");
                        return '请上传商品详情图';
                    }
                }
            });

            form.on('radio(goodsCate)', function(data){
                //goodsCate(data.value);
            });

           // goodsCate($('#goods_type').val());
            function goodsCate(val){
                var option = JSON.parse($('#cateJson').val());
                var cate_id = $('#cate_id').val();
                if(val == 1 || val == 3){
                    var typearray = option.ordinary;
                }else{
                    var typearray = option.integral;
                }
                var html = '<option value="">选择分类</option>';
                var seled = '';
                $.each(typearray,function(i,n){
                    if(cate_id == n.id){
                        seled = 'selected';
                    }
                    html += '<option value="'+n.id+'" '+seled+'>'+n.name+'</option>';
                });

                $("#optionCate").html(html);
                form.render('select');
            }

            //监听提交
            form.on('submit(add)', function (data) {
                $.ajax({
                    url: js_url('shop-goods-update'),
                    type: 'post',
                    data: data.field,
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            layer.msg(result.msg, {time: 600, icon: 6}, function () {
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        } else {
                            layer.msg(result.msg, {time: 600, icon: 5});
                        }
                    }
                });
                return false;
            });

        });
    </script>
{{--    <script type="text/javascript">
        $(document).ready(function () {
            var formSelects = layui.formSelects;
            var tag = "{{$arr_data['sale_tag_relation']}}";
            formSelects.render('sale_tag', {
                init: tag.split(","),  //默认值
            });
        });
    </script>--}}
@endsection




